pkg_name=glibc
version=2.21
build_dir=$builds_dir_root/$pkg_name-cross-$version
src_dir=$src_dir_root/$pkg_name-$version
url=http://mirror-fr1.bbln.org/gnu/libc/$pkg_name-$version.tar.xz

#######################################
#does not install locales or timezones#
#######################################

rm -Rf "$src_dir"
cd "$src_dir_root"
tar Jxf "$pkg_name-$version.tar.xz"

#-------------------------------------------------------------------------------
#In many places in the installation run, subpaths are hardcoded disregarding
#the ones provided on configure command line. Do work around that mess.

#move back iconvconfig to /bin
cd "$src_dir/iconv"
cp "$builders_dir_root/$pkg_name/iconv-Makefile.patch" ./Makefile.patch
patch -Ni ./Makefile.patch

#move back zic and zdump to /bin
cd "$src_dir/timezone"
cp "$builders_dir_root/$pkg_name/timezone-Makefile.patch" ./Makefile.patch
patch -Ni ./Makefile.patch

#move back nscd to /bin
cd "$src_dir/nscd"
cp "$builders_dir_root/$pkg_name/nscd-Makefile.patch" ./Makefile.patch
patch -Ni ./Makefile.patch

rm -Rf "$build_dir"
mkdir -p "$build_dir"
cd "$build_dir"

#works only if prefix is not /usr (sysdeps will hardcode it to /sbin if /usr)
echo "libc_cv_rootsbindir=/bin" >"$build_dir/config.cache"
#-------------------------------------------------------------------------------

#A turd from the official devs decided to force optimization to a minimum of -O2
"$src_dir/configure"				\
	"--cache-file=$build_dir/config.cache"	\
	--host=$gnu_target			\
	--prefix=/				\
	--exec-prefix=/				\
	--bindir=/bin				\
	--sbindir=/bin				\
	--libexecdir=/libexec			\
	--sysconfdir=/etc			\
	--localstatedir=/var			\
	--libdir=/lib				\
	--includedir=/include			\
	--datarootdir=/share			\
	--datadir=/share			\
	--localedir=/share/locale		\
	"--with-headers=$sysroot/include"	\
	"--enable-kernel=$oldest_linux_version"

make

install_tmp_dir=/tmp/$pkg_name-cross-$version
rm -Rf "$install_tmp_dir"
mkdir -p "$install_tmp_dir"

make install "DESTDIR=$install_tmp_dir"

install_dirs_in_sysroot "$builders_dir_root/$pkg_name/runtime-cross.dirs" "$sysroot/"
install_files_in_sysroot "$builders_dir_root/$pkg_name/runtime-cross.files" "$install_tmp_dir/" "$sysroot/"
install_links_in_sysroot "$builders_dir_root/$pkg_name/runtime-cross.links" "$sysroot/"
install_dirs_in_sysroot "$builders_dir_root/$pkg_name/devel-cross.dirs" "$sysroot/"
install_files_in_sysroot "$builders_dir_root/$pkg_name/devel-cross.files" "$install_tmp_dir/" "$sysroot/"
install_links_in_sysroot "$builders_dir_root/$pkg_name/devel-cross.links" "$sysroot/"
elf_strip_in_sysroot "$builders_dir_root/$pkg_name/elf-strip-cross" "$sysroot/"

rm -Rf "$install_tmp_dir"
rm -Rf "$build_dir"
rm -Rf "$src_dir"

#fixing bash-isms 
sed -i 's/bash/sh/' "$sysroot/bin/ldd"
sed -i 's/bash/sh/' "$sysroot/bin/sotruss"
sed -i 's/bash/sh/' "$sysroot/bin/tzselect"
sed -i 's/bash/sh/' "$sysroot/bin/xtrace"
